package com.wondertek.service.sdk.iotdh.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.wondertek.service.sdk.iotdh.config.SdkConfig;
import com.wondertek.common.util.base.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class SdkUtil {
    private static final String SIGN_TEMPLATE = "time:%s,nonce:%s,clientSecret:%s";

    /**
     * 创建签名
     *
     * @param time  时间戳
     * @param nonce 随机数
     */
    public String createSign(long time, String nonce, SdkConfig sdkConfig) {
//        String SIGN_TEMPLATE = MessageFormat.format("time:{0},nonce:{1},appSecret:{2}",
        String signTemplate = String.format(SIGN_TEMPLATE,
                time, nonce, sdkConfig.getClientSecret());
        Date date = new Date();
        log.info("clientId->{},当前时间->{},签名源串->{}",
                ObjectUtil.isNotEmpty(sdkConfig) ? sdkConfig.getClientId() : "",
                DateUtil.date2LongString(date),
                signTemplate);

        String sign = DigestUtil.md5Hex(signTemplate);
        log.info("clientId->{},当前时间->{},签名->{}",
                ObjectUtil.isNotEmpty(sdkConfig) ? sdkConfig.getClientId() : "",
                DateUtil.date2LongString(date),
                sign);
        return sign;
    }

}
